//
//  HTTravelDetailViewController.m
//  HeartTravel
//
//  Created by lanou on 15/10/31.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTTravelDetailViewController.h"
#import "HTWaypointsModel.h"
#import "HTTravelDetailCell.h"
#import "HTTravelDetailModel.h"
#import "PKRequestManager.h"
#import "HTTravelPicCell.h"

@interface HTTravelDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSoureArray;

@end

@implementation HTTravelDetailViewController
- (NSMutableArray *)dataSoureArray {
    
    if (_dataSoureArray == nil) {
        _dataSoureArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSoureArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text = self.name;
    self.titleLabel.font = [UIFont systemFontOfSize:12];
    self.titleLabel.numberOfLines = 0;
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(handleReturnButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT - 70)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [_mainTableView registerClass:[HTTravelDetailCell class] forCellReuseIdentifier:@"cell"];
    [_mainTableView registerClass:[HTTravelPicCell class] forCellReuseIdentifier:@"picCell"];
    
    [self.view addSubview:_mainTableView];
    [self requestData];
    
}

- (void)requestData {
    
    NSString *urlStr = [@"http://api.breadtrip.com/trips/" stringByAppendingFormat:@"%@%@", self.id_name, @"/waypoints/"];
    [PKRequestManager requestWithType:GET urlString:urlStr parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *modelDic in dict[@"days"]) {
            HTTravelDetailModel *model = [[HTTravelDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.dataSoureArray addObject:model];
        }
        [self.mainTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSoureArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    HTTravelDetailModel *model = _dataSoureArray[section];
    NSString * string = [model.date stringByAppendingFormat:@" 第%@天", model.day];
    return string;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    HTTravelDetailModel *model = _dataSoureArray[section];
    NSMutableArray *arr = model.waypoints;
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HTTravelDetailModel *model = _dataSoureArray[indexPath.section];
    HTWaypointsModel *wmodel = model.waypoints[indexPath.row];
    if ([wmodel.photo isEqualToString:@""]) {
        
        HTTravelDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setConfigureModel:wmodel];
        return cell;
    } else {
        
        HTTravelPicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"picCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setConfigureModel:wmodel];
        return cell;
        
    }
}

- (void)handleReturnButtonAction:(id)sender {
    
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = @"suckEffect";
    //animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromLeft;
    [self.view.window.layer addAnimation:animation forKey:nil];
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HTTravelDetailModel *model = _dataSoureArray[indexPath.section];
    HTWaypointsModel *wmodel = model.waypoints[indexPath.row];
    
    if ([wmodel.photo isEqualToString:@""]) {
        return [HTTravelDetailCell heightForCellWithModel:wmodel];
    } else {
        return [HTTravelPicCell heightForCellWithModel:wmodel];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
